package com.sooncode.project.focusservice.model;

import com.alibaba.excel.annotation.ExcelProperty;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.springcloudcommon.utils.DateUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class ClassReport {

    @Data
    public class TestListReport {
        private int serial = 0;
        @ExcelProperty("姓名")
        private String name;
        @ExcelProperty("性别")
        private String sex;
        @ExcelProperty("年龄")
        private String age;
        @ExcelProperty("班级")
        private String className;
        @ExcelProperty("评测类型")
        private String testType;
        @ExcelProperty("评测老师")
        private String teacher;
        @ExcelProperty("评测时间")
        private String testDate;
        private String item1;
        private String item2;
        private String item3;
        private String item4;
        private String item5;
        private String item6;
        private String item7;
        private String item8;
        private String item9;

    }

    @Data
    public class TestAnalysisReport {
        private String itemName;
        private String avg;
        private String l1;
        private String l2;
        private String l3;
        private String l4;
        private String l5;
    }

    @Data
    public class AnalysisHelper {
        private double sumScore = 0;
        private double l1 = 0;
        private double l2 = 0;
        private double l3 = 0;
        private double l4 = 0;
        private double l5 = 0;
        private String name;
    }

    public Map getTestListData(String classId) {
        List<Test> tests = new Finder<>(Test.class).byField("classId", classId).list();
        if (tests == null || tests.size() == 0) return null;
        List<TestListReport> reportItems = new ArrayList<>();
        int i = 0;
        Map<String, AnalysisHelper> anaMap = new HashMap<>();
        double sum=0;
        for (Test test : tests) {
            TestListReport reportItem = new TestListReport();
            reportItem.serial = ++i;
            reportItem.name = test.getName();
            reportItem.sex = test.getSex();
            reportItem.age = test.getAge();
            reportItem.className = test.getClassName();
            reportItem.testType = test.getCheck_type();
            reportItem.teacher = test.getTeacher();
            reportItem.testDate = DateUtils.formatDate(test.getTestDate());
            List<TestData> testDatas = test.getData();
            sum+=test.getAverageScore();
            for (TestData testData : testDatas) {
                if (testData.getItem_no().equals("1"))
                    reportItem.item1 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("2"))
                    reportItem.item2 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("3"))
                    reportItem.item3 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("4"))
                    reportItem.item4 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("5"))
                    reportItem.item5 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("6"))
                    reportItem.item6 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("7"))
                    reportItem.item7 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("8"))
                    reportItem.item8 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                else if (testData.getItem_no().equals("9"))
                    reportItem.item9 = testData.getLevelScore() + "分(" + testData.getLevel() + ")";
                addAnalysisItem(anaMap, testData);
            }
            reportItems.add(reportItem);
        }
        Map data = new HashMap();
        data.put("total", tests.size());
        data.put("className", tests.get(0).getClassName());
        Map result = new HashMap();
        result.put("items", reportItems);

        List<TestAnalysisReport> analysisItems = new ArrayList<>();

        for (String key : anaMap.keySet()){
            TestAnalysisReport anaItem=new TestAnalysisReport();
            anaItem.setItemName(anaMap.get(key).name);
            double avg=Utils.doubleFormat1(anaMap.get(key).sumScore/tests.size());
            anaItem.setAvg(avg+"分("+getItemName(avg)+")");
            anaItem.setL1(getAnalysisString(anaMap.get(key).l1,tests.size()));
            anaItem.setL2(getAnalysisString(anaMap.get(key).l2,tests.size()));
            anaItem.setL3(getAnalysisString(anaMap.get(key).l3,tests.size()));
            anaItem.setL4(getAnalysisString(anaMap.get(key).l4,tests.size()));
            anaItem.setL5(getAnalysisString(anaMap.get(key).l5,tests.size()));
            analysisItems.add(anaItem);
        }
        result.put("analysisItems", analysisItems);
        double allAvg=Utils.doubleFormat1(sum/tests.size());
        data.put("allavg",allAvg+"分("+getItemName(allAvg)+")");
        result.put("data", data);
        return result;
    }
    private String getItemName(double score){
        if(score>=0&&score<=1.49){
            return "极弱";
        }else if(score>=1.5&&score<=2.49){
            return "较弱";
        }else if(score>=2.5&&score<=3.49){
            return "不足";
        }else if(score>=3.5&&score<=4.49){
            return "中等";
        }else if(score>=4.5){
            return "优秀";
        }
        return "";
    }

    private String getAnalysisString(double count,int allCount) {
        double v=count/allCount*100;
        String result = ((int)count)+"人("+(int)v+"%)";
        return result;
    }

    private void addAnalysisItem(Map<String, AnalysisHelper> map, TestData testData) {
        if (!map.containsKey(testData.getItem_no())) {
            AnalysisHelper helper=new AnalysisHelper();
            helper.setName(testData.getItemName());
            map.put(testData.getItem_no(), helper);
        }
        map.get(testData.getItem_no()).sumScore += testData.getLevelScore();
        switch (testData.getLevel()) {
            case "极弱":
                map.get(testData.getItem_no()).l1++;
                break;
            case "较弱":
                map.get(testData.getItem_no()).l2++;
                break;
            case "不足":
                map.get(testData.getItem_no()).l3++;
                break;
            case "中等":
                map.get(testData.getItem_no()).l4++;
                break;
            case "优秀":
                map.get(testData.getItem_no()).l5++;
                break;
        }
    }
}
